// 院管理员设置板块
import {
    getAdmin,
    deleteAdmin,
    deletesAdmin,
    addAdmin,
    modifyAdmin,
    addNewAdmin
} from "../../api/collegeAdmin.js";
import {getColleges} from "../../api/collegeAdmin";

const state = () => ({
    allAdminData: [], // 用于存储所有管理员数据
    collegeAdminRecord: [],
    colleges: []
})

const mutations = { // mutations改变state中的数据
    // 设置管理员记录
    SET_COLLEGE_ADMIN: (state, data) => { // 存储token
        state.collegeAdminRecord = data
    },
    SET_COLLEGES: (state, data) => { // 存储token
        state.colleges = data
    },
    SET_ALL_COLLEGE_ADMIN: (state, data) => {
        state.allAdminData = data
    },
    // 删除管理员
    DELETE_ADMIN: () => {
        // for(var i = 0; i < state.collegeAdminRecord.length; i ++){
        //     if(state.collegeAdminRecord[i].collegeAdminId === id){
        //         state.collegeAdminRecord.splice(i, 1)
        //         break;
        //     }
        // }
    },
    // 批量删除管理员
    DELETE_ADMINS: () => {
        // for(var i = 0; i < state.collegeAdminRecord.length; i ++){
        //     if(ids.indexOf(state.collegeAdminRecord[i].collegeAdminId) !== -1){
        //         state.collegeAdminRecord.splice(i, 1)
        //         // 因为删除后数组大小会改变，所以我们需要回退1
        //         i--
        //     }
        // }
    },
    // 搜索管理员信息
    SEARCH_ADMIN: (state, key) => {
        // 我们直接使用数组的过滤来进行内容的搜索
        state.collegeAdminRecord = state.allAdminData.filter(item => item.name.indexOf(key) !== -1)
    },
    // 添加管理员信息
    ADD_ADMIN: (state, key) => {
        state.collegeAdminRecord.splice(0, 0,key)
    },
    // 修改管理员信息
    CHANGE_ADMIN: (state, data) => {
        state.collegeAdminRecord=state.collegeAdminRecord.map(item => (item.collegeAdminId === data.collegeAdminId ? data:item ))
    },
    NULL:()=>{}

}

const actions = { // 发送请求
    // 获取所有的学院信息
    getColleges({ commit }, data){
        return new Promise((resolve, reject) => {
            getColleges(data).then( data => {
                if(data.code === 1){ // 登录成功
                    commit('SET_COLLEGES', data.data)
                }
                // console.log("获取到的管理员的数据", data)
                resolve(data);
            } ).catch(error => {
                reject(error)
            })
        })
    },
    // 发送请求获取管理员信息
    getCollegeAdmin({ commit }, data){
        return new Promise((resolve, reject) => {
            getAdmin(data).then( data => {
                if(data.code === 1){ // 登录成功
                    commit('SET_COLLEGE_ADMIN', data.data)
                    commit('SET_ALL_COLLEGE_ADMIN', data.data)
                }
                // console.log("获取到的管理员的数据", data)
                resolve(data);
            } ).catch(error => {
                reject(error)
            })
        })
    },
    // 删除院管理员界面
    deleteCollegeAdmin({ commit },id){
        return new Promise((resolve, reject) => {
            deleteAdmin(id).then( res => {
                if (res.code === 1){
                    commit('DELETE_ADMIN',id)
                    resolve(res.msg)
                } else {
                    reject(res.msg)
                }
            })
        })
    },
    // 批量删除管理员
    deleteCollegeAdmins({ commit },ids){
        return new Promise((resolve, reject) => {
            deletesAdmin(ids).then( res => {
                if (res.code === 1){
                    commit('DELETE_ADMINS',ids)
                    resolve(res.msg)
                } else {
                    reject(res.msg)
                }
            })
        })
    },
    // 搜索管理员信息
    searchAdminInfo({ commit },key){
        commit('SEARCH_ADMIN',key)
    },
    // 添加管理员信息
    addAdminInfo({ commit },data){
        return new Promise((resolve, reject) => {
            addAdmin(data).then( res => {
                if (res.code === 1){
                    commit('ADD_ADMIN',data)
                    resolve(res.msg)
                } else {
                    reject(res.msg)
                }
            })
        })
    },
    // 修改管理员信息
    changeAdminInfo({ commit }, data){
        return new Promise((resolve, reject) => {
            modifyAdmin(data).then( res => {
                if (res.code === 1){
                    commit('CHANGE_ADMIN',data)
                    resolve(res.msg)
                } else {
                    reject(res.msg)
                }
            })
        })
    },
    addNewAdmin({ commit },data){
        return new Promise((resolve, reject) => {
            addNewAdmin(data).then( res => {
                if (res.code === 1){
                    commit('NULL')
                    resolve(res.msg)
                } else {
                    reject(res.msg)
                }
            })
        })
    },
}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}
